/**
 * 
 */
package se.cirrus.base;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

import se.cirrus.base.com.Controller;
import se.cirrus.base.com.ControllerThread;
import se.cirrus.base.struct.Content;

/**
 * @author Winquman
 *
 */
public class ExampleChat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LogManager lm = LogManager.getLogManager();
			
			Formatter formatter = new CirrusLogFormatter();
			Formatter cFormatter = new CirrusConsoleFormatter();
			
			
			FileHandler fileHandler = new FileHandler("cirrus-chat%g.log", true);
			fileHandler.setFormatter(formatter);
			StreamHandler consoleHandler = new StreamHandler(System.out, formatter);
			consoleHandler.setFormatter(formatter);
			
			fileHandler.setLevel(Level.ALL);
			consoleHandler.setLevel(Level.INFO);
			
			Logger log = Logger.getLogger("ALL");
			log.addHandler(fileHandler);
			log.addHandler(consoleHandler);
			log.setLevel(Level.ALL);
			
			System.out.println(log.isLoggable(Level.FINE));
			System.out.println(fileHandler.getLevel());
			
			log.info("\n\n===========================================");
			log.info("Cirrus Base - Example Chat test");
			log.fine("Logs initialized.");
			
			log.info("Initializing content queues...");
			
			ConcurrentLinkedQueue<Content> up = new ConcurrentLinkedQueue<Content>();
			ConcurrentLinkedQueue<Content> down = new ConcurrentLinkedQueue<Content>();
			
			log.info("Content queues initialized.");
			
			log.info("Initializing Controller....");
			
			Controller cont = new Controller(null, down, up);
			
			log.info("Controller initialized.");
			
			log.info("Initializing Controller thread...");
			
			ControllerThread ct = new ControllerThread(cont);
			
			log.info("Starting Controller thread: " + ct);
			ct.start();
			
			log.info("Controller thread running!");
			
			ChatProgram cp = new ChatProgram(down, up);
			
			log.fine("Entering the chat program interface.");
			cp.interfaceLoop();
			
			cont.gracefulStop();
			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
		

	}

}
